package com.ibm.spatiotemp.server.datasources;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.databind.JsonNode;
import com.ibm.spatiotemp.tools.jsonParser;
import com.ibm.spatiotemp.utils.JSonInterface;

public class DataSchema extends JSonInterface {
	
	public static Logger logger = Logger.getLogger(DataSchema.class);
	
	protected String tab_name;
	protected String x_colname;
	protected String y_colname;
	protected String ts_colname;
	protected String x_coltype;
	protected String y_coltype;
	protected String ts_coltype;
	protected String movobj_colname;
	protected String movobj_coltype;
	protected String other;
	
	public String getMovobj_colname() {
		return movobj_colname;
	}
	public void setMovobj_colname(String movobj_colname) {
		this.movobj_colname = movobj_colname;
	}
	public String getMovobj_coltype() {
		return movobj_coltype;
	}
	public void setMovobj_coltype(String movobj_coltype) {
		this.movobj_coltype = movobj_coltype;
	}
	
	public String getTab_name() {
		return tab_name;
	}
	public void setTab_name(String tab_name) {
		this.tab_name = tab_name;
	}
	public String getX_colname() {
		return x_colname;
	}
	public void setX_colname(String x_colname) {
		this.x_colname = x_colname;
	}
	public String getY_colname() {
		return y_colname;
	}
	public void setY_colname(String y_colname) {
		this.y_colname = y_colname;
	}
	public String getTs_colname() {
		return ts_colname;
	}
	public void setTs_colname(String ts_colname) {
		this.ts_colname = ts_colname;
	}
	public String getX_coltype() {
		return x_coltype;
	}
	public void setX_coltype(String x_coltype) {
		this.x_coltype = x_coltype;
	}
	public String getY_coltype() {
		return y_coltype;
	}
	public void setY_coltype(String y_coltype) {
		this.y_coltype = y_coltype;
	}
	public String getTs_coltype() {
		return ts_coltype;
	}
	public void setTs_coltype(String ts_coltype) {
		this.ts_coltype = ts_coltype;
	}
	public String getOther() {
		return other;
	}
	public void setOther(String other) {
		this.other = other;
	}
	
	@Override
	public void buildInstanceByJsonString(String jsonstr) {
		JsonNode node = jsonParser.fromString(jsonstr);
		buildInstanceByJsonString(node);
	}
	
	@Override
	public void buildInstanceByJsonString(JsonNode node) {
		Class<?> classType = this.getClass();
        // 鑾峰緱瀵硅薄鐨勬墍鏈夊睘鎬�
        Field fields[] = classType.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            String fieldName = field.getName();
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
        // 鑾峰緱鍜屽睘鎬у搴旂殑setXXX()鏂规硶
        Method setMethod;
        JsonNode valuenode = node.get(fieldName);
        Object value = null;
        if(valuenode!=null)
         value = (Object) valuenode.asText();
        // 鑾峰緱鍜屽睘鎬у搴旂殑setXXX()鏂规硶鐨勫悕瀛�
        String setMethodName = "set" + firstLetter + fieldName.substring(1);
        try {
        	if(value != null){
			setMethod = classType.getMethod(setMethodName, new Class[]{field.getType()});
	        // 璋冪敤鎷疯礉瀵硅薄鐨剆etXXX()鏂规硶
	        setMethod.invoke(this, new Object[]{value});
        	}
		} catch (NoSuchMethodException e) {
//			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
        
        }

	}
	
	@Override
	public String toJsonFormat() {
		Map<String,Object> instmap = ClassReflectionToolkit.getInstancesFields(this);
		return ClassReflectionToolkit.mapToJsonString(instmap);
	}
}
